package aishua.com.mytestapplist;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PersistableBundle;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import aishua.com.mytestapplist.utils.LogUtils;
import chinatelecom.mwallet.open.service.INFCOpenAbility;

/**
 * Created by aishua on 2018/5/8.
 */

public class OpenNFCActivity extends Activity {
    private static final String TAG = "OpenNFCActivity-";
    private Button btn_init;
    private Button btn_login;
    private Button btn_userAuth;
    private Button btn_appletOpr;
    private Button btn_bind;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_openfc);

        btn_init = (Button) findViewById(R.id.btn_init);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_userAuth = (Button) findViewById(R.id.btn_userAuth);
        btn_appletOpr = (Button) findViewById(R.id.btn_appletOpr);
        btn_bind = (Button) findViewById(R.id.btn_bind);

        click();
    }

    private void click() {
        btn_bind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                bind();
            }
        });
        btn_init.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread() {
                    @Override
                    public void run() {
//                          江苏一卡通spid
                        try {
                            String init = abi.init("35900276", "chinatelecom.mwallet.open");
                            LogUtils.v(TAG + "init:" + init);
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }

                    }
                }.start();
            }
        });
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread() {
                    @Override
                    public void run() {


                    }
                }.start();
            }
        });
        btn_userAuth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread() {
                    @Override
                    public void run() {


                    }
                }.start();
            }
        });
        btn_appletOpr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread() {
                    @Override
                    public void run() {


                    }
                }.start();
            }
        });


    }

    private void bind() {

        Intent i = new Intent();
        i.setAction("chinatelecom.mwallet.open.service.SeOprService");
        i.setPackage("aishua.com.mytestapplist");
        i.setPackage("chinatelecom.mwallet.open");        //startService(i);i
        bindService(i, conn, Activity.BIND_AUTO_CREATE);
    }

    INFCOpenAbility abi;
    private ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {


        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            abi = INFCOpenAbility.Stub.asInterface(service);
            LogUtils.v("bind service success");
            Toast.makeText(getApplicationContext(), "绑定服务成功", Toast.LENGTH_SHORT).show();

        }
    };

}
